﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ProjetPacman;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pacman
{
    class Levels
    {
        public const int VX = 31;
        public const int VY = 28;
        private int[,] map;
        public const int WALL = 1;
        public const int CLEAN = 0;
        public const int BEAN = 2;
        public const int PACGUM = 3;
        public const int FANTOME = 4;

        private Frame mur;
        private Frame bean;
        private Frame pacgum;

        public int[,] getMap()
        {
            return map;
        }

        public int[,] getMapFantome(bool eated)
        {
            int [,] map2= new int [VX,VY];
            for (int i = 0; i < VX; i++)
            {
                for (int j = 0; j < VY; j++)
                {
                    if (map[i, j] == CLEAN || map[i, j] == BEAN || map[i, j] == PACGUM)
                    {
                        map2[i, j] = 1;
                    }
                    else if (eated && map[i, j] >= FANTOME)
                    {
                        map2[i, j] = 1;
                    }
                    else map2[i, j] = 0;

                    if (i == 0 || j == 0 || i == VX - 1 || j == VY - 1) map2[i, j] = 0;
                }
            }
            return map2;
        }

        public int getBlock(Vector2 position)
        {
            return map[(int)position.Y, (int)position.X];
        }

        public void posFantome(Vector2 position)
        {
            if (map[(int)position.Y, (int)position.X]>=FANTOME)map[(int)position.Y, (int)position.X]-=FANTOME;
            else map[(int)position.Y, (int)position.X] += FANTOME;
        }

        public int eatBlock(Vector2 position)
        {
            int block = map[(int)position.Y, (int)position.X];
            map[(int)position.Y, (int)position.X]=0;
            return block;
        }

        public bool collision(int x, int y)
        {
            // on inverse les coordonnées
            return (map[y, x] == WALL);
        }

        public void reset()
        {
            for (int i = 0; i < VX; i++)
            {
                for (int j = 0; j < VY; j++)
                {
                    if (map[i, j] >= 4) map[i, j] -= 4;
                }
            }
        }

        public Levels(ContentManager content, int niveau)
        {
            map = new int[VX, VY]{
                    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                    {1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
                    {1, 3, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1},
                    {1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1},
                    {1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1},
                    {1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
                    {1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1},
                    {1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1},
                    {1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1},
                    {1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1},
                    {1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1},
                    {1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
                    {1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 0, 0, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
                    {1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 0, 0, 0, 0, 0, 0, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
                    {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 4, 0, 0, 0, 0, 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
                    {1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 4, 0, 0, 0, 0, 4, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
                    {1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
                    {1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
                    {1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
                    {1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
                    {1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
                    {1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1},
                    {1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1},
                    {1, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 1},
                    {1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1},
                    {1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1},
                    {1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1},
                    {1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1},
                    {1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1},
                    {1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
                    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
                };

            // on charge un objet mur 
            mur = new Frame(content.Load<Texture2D>("Images/mur"), new Rectangle(0,0,20,20));
            // on charge un objet bean
            bean = new Frame(content.Load<Texture2D>("Images/bean"), new Rectangle(0, 0, 20, 20));

            pacgum = new Frame(content.Load<Texture2D>("Images/pacgum"), new Rectangle(0, 0, 20, 20));
        }

        public Levels()
        {

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            for (int x = 0; x < VX; x++)
            {
                for (int y = 0; y < VY; y++)
                {
                    int xpos, ypos;
                    xpos = x * 20;
                    ypos = y * 20;
                    Vector2 pos = new Vector2(ypos, xpos);
                    if (map[x, y] == WALL)
                    {
                        spriteBatch.Draw(mur.Texture, pos, Color.White);
                    }
                    else if (map[x, y] == BEAN)
                    {
                        spriteBatch.Draw(bean.Texture, pos, Color.White);
                    }
                    else if (map[x, y] == PACGUM)
                    {
                        spriteBatch.Draw(pacgum.Texture, pos, Color.White);
                    }
                }

            }
        }
    }
}
